/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("PurchaseNote",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using BusFin.Read.Entity.DomainModels;
using BusFin.Read.System.IServices;
using SqlSugar;
using Microsoft.AspNetCore.Authorization;
using VOL.Entity.DomainModels;
using VOL.Entity.DTO;
using System.Linq;

namespace BusFin.Read.System.Controllers
{
    public partial class PurchaseNoteController
    {
        private readonly IPurchaseNoteService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ISqlSugarClient _db;

        [ActivatorUtilitiesConstructor]
        public PurchaseNoteController(IPurchaseNoteService service, IHttpContextAccessor httpContextAccessor, ISqlSugarClient db) : base(service)
        {
            _db = db;
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        [HttpGet, Route("SelectProductList"), AllowAnonymous]
        public IActionResult GetProductList()
        {
            var list = _db.Queryable<VOL.Entity.DomainModels.Product>().ToList();
            return Ok(list);
        }


        [HttpGet, Route("SelectPurchaseList"), AllowAnonymous]
        public (int totalCount, int pageCount, List<PurchaseList> list) GetOrderList(int page, int size, string? purchaseCode, string? productCode, string? productName, int orderState)
        {
            var query = _db.Queryable<PurchaseNote>()
                          .LeftJoin<PurchaseProductDetails>((o, og) => o.PurchaseId == og.PurchaseId)
                          .LeftJoin<VOL.Entity.DomainModels.Product>((o, og, g) => og.ProductId == g.ProductId)
                          .Where(o => o.IsDeleted == 0)
                          .WhereIF(!string.IsNullOrWhiteSpace(purchaseCode), (o, og, g) => o.PurchaseId.ToString().Contains(purchaseCode))
                          .WhereIF(!string.IsNullOrWhiteSpace(productCode), (o, og, g) => og.ProductId.Contains(productCode))
                          .WhereIF(!string.IsNullOrWhiteSpace(productName), (o, og, g) => og.ProductName.Contains(productName))
                          .WhereIF(orderState != 0, (o, og, g) => o.OrderStatus == orderState)
                          .Select((o, og, g) => new { o, og, g })
                          .Distinct();

            int totalCount = query.Count();
            int pageCount = (int)Math.Ceiling(totalCount * 1.0 / size);

            var orders = query
                  .Skip((page - 1) * size)
                  .Take(size)
                  .ToList();

            var listDTO = orders.Select(c => new PurchaseList
            {
                PurchaseId = c.o.PurchaseId,
                ProductName = c.og.ProductName,
                ProductId = c.og.ProductId,
                Specification = c.og.Specification,
                UnitId = c.og.Unit,
                SupplierId = c.g.SupplierId,
                Price = c.g.Price,
                ProductNum = c.og.ProductNum,
                ProductTotal = c.og.ProductTotal,
                IsEntry = c.o.IsEntry,
                IsSendBack = c.o.IsSendBack,
                IsDeleted = c.o.IsDeleted,
                CreateTime = c.o.CreateTime
            }).ToList();

            return (totalCount, pageCount, listDTO);
        }

    }
}
